﻿///***************************************************************
/// 시  스  템 : Updms.Mny
/// 단위시스템 : MNY
/// 계      층 : Presentation Layer
/// 프로그램명 : Mny2020r
/// 파  일  명 : Mny2020r.aspx.cs
/// 설      명 : 내용별챠트(월별)
///***************************************************************
/// 수정ID 일자       이름     내용
///---------------------------------------------------------------
/// CH001  2009-02-04 송시명   최초 작성
///***************************************************************
using System;
using System.Data;
using System.Web.UI;
using System.Web.UI.WebControls;

using SMFramework.Data;
using SMFramework.Utility;

using Base.Dac;
using Updms.Mny.Exe;


namespace Updms.Mny
{
	public partial class Mny2020r : Updms.Common.BaseClass.PageBase
	{
		#region 전역영역

		//프로그램상수
		public static readonly string PID = "Mny2020r";
		public static readonly string gs_path_info = "/updms/Mny/" + PID + ".aspx";

		Global global = new Global();
		CommCode.Mny mny = new CommCode.Mny(LANG_SET);

		//검색
		protected string kw1 = "";	// 조회시작일
		protected string kw2 = "";	// 조회종료일
		protected string kw3 = "";	// 내용

		//메뉴 선택값
		protected string gs_sm = "";

		// 페이지모드
		private string gs_page_mode = "";

		//합계
		int li_total_amount;
		int li_total_count;

		#endregion

		#region Page_Load, OnPageLoad

		protected void Page_Load(object sender, EventArgs e)
		{
			SetInit();
			if (!Page.IsPostBack)
			{
				// 최초 화면 로딩시 처리해야 할 부분
				OnPageLoad();
			}
		}

		// OnPageLoad
		private void OnPageLoad()
		{
			SetLiterals(global.GetLabelInfoByPgmId(PID, LANG_SET));
			SetClientScript();
			SetSearchAccount();
			SetSearchParameter();
		}

		#endregion

		#region InitializeComponent

		private void InitializeComponent()
		{
			this.Load += new System.EventHandler(this.Page_Load);
			this.PreRender += new System.EventHandler(this.Page_PreRender);
		}

		#endregion

		#region SetClientScript

		private void SetClientScript()
		{
			//엔터검색
			txt_search_from_dt.Attributes.Add("onkeypress", "enterSearchOnPage(event)");
			txt_search_to_dt.Attributes.Add("onkeypress", "enterSearchOnPage(event)");
			txt_contents_card.Attributes.Add("onkeypress", "enterSearchOnPage(event)");
			txt_contents_money.Attributes.Add("onkeypress", "enterSearchOnPage(event)");
		}

		#endregion

		#region SetInit, SetSearchParameter

		//초기값설정
		private void SetInit()
		{
			lbl_search_flag.Text = "INIT";

			gs_sm = (Request.QueryString["sm"] == null || Request.QueryString["sm"].Trim().Length != 7)
					? global.GetDirDepth(PID) : Request.QueryString["sm"].ToString();
		}

		//검색파라미터
		private void SetSearchParameter()
		{
			switch (lbl_search_flag.Text.Trim().ToUpper())
			{
				case "INIT":					
					if (String.IsNullOrEmpty(Request.QueryString["kw1"]))
					{
						if (String.IsNullOrEmpty(txt_search_from_dt.Text))
						{
							kw1 = DateUtil.GetMonthEx(-12);	//오늘부터 12개월전
							txt_search_from_dt.Text = kw1;
						}
						else
						{
							kw1 = txt_search_from_dt.Text.Trim();
						}
					}
					else
					{
						kw1 = Server.UrlDecode(Request.QueryString["kw1"]);
						txt_search_from_dt.Text = kw1;
					}

					if (String.IsNullOrEmpty(Request.QueryString["kw2"]))
					{
						if (String.IsNullOrEmpty(txt_search_to_dt.Text))
						{
							kw2 = DateUtil.GetMonthEx(0);	//오늘
							txt_search_to_dt.Text = kw2;
						}
						else
						{
							kw2 = txt_search_to_dt.Text.Trim();
						}
					}
					else
					{
						kw2 = Server.UrlDecode(Request.QueryString["kw2"]);
						txt_search_to_dt.Text = kw2;
					}

					//if (String.IsNullOrEmpty(Request.QueryString["kw3"]))
					//{
					//	kw3 = txt_contents_card.Text.Trim();
					//}
					//else
					//{
					//	kw3 = Server.UrlDecode(Request.QueryString["kw3"]);
					//	txt_search_contents.Text = kw3;
					//}

					kw3 = txt_contents_card.Visible ? txt_contents_card.Text.Trim() : txt_contents_money.Text.Trim();
					
					break;
				case "SEARCH":
					kw1 = String.IsNullOrEmpty(txt_search_from_dt.Text) ? DateUtil.GetMonthEx(-12) : txt_search_from_dt.Text.Trim();
					kw2 = String.IsNullOrEmpty(txt_search_to_dt.Text) ? DateUtil.GetMonthEx(0) : txt_search_to_dt.Text.Trim();
					kw3 = txt_contents_card.Visible ? txt_contents_card.Text.Trim() : txt_contents_money.Text.Trim();
					break;
			}
		}


		#endregion

		#region 마스터 Method (SetSearchAccount, SetYearMonth)

		//조회대상
		private void SetSearchAccount()
		{
			mny.SetSearchAccount(rbl_search_account);
		}

		//검색조건의 년월설정 - 기간지정
		private void SetYearMonth(int li_term)
		{
			txt_search_from_dt.Text = DateUtil.GetMonthEx(li_term);
			txt_search_to_dt.Text = DateUtil.GetMonthEx(0);
		}

		#endregion

		#region 디테일 Method ()

		//조회대상변경시
		protected void rbl_search_account_SelectedIndexChanged(object sender, EventArgs e)
		{
			if (rbl_search_account.SelectedIndex == 0)
			{
				txt_contents_card.Visible = true;
				txt_contents_money.Visible = false;
			}
			else
			{
				txt_contents_card.Visible = false;
				txt_contents_money.Visible = true;
			}
		}

		#endregion

		#region 챠트 (DrawMonthlyTotalAmountByContents, SearchProcess)

		//조회
		private void DrawMonthlyTotalAmountByContents()
		{
			int li_count = 0;
			DataSet lo_ds = new DataSet();
			DataPack lo_dp = new DataPack();

			//합계(초기화)
			li_total_amount = 0;
			li_total_count = 0;

			//초기화
			chart_line.Data.Clear();

			//시리즈 갯수
			chart_line.Data.Series = 2; //금액,건수			

			//검색
			lo_dp.AddProperty("ls_search_from_dt", typeof(string), kw1);
			lo_dp.AddProperty("ls_search_to_dt", typeof(string), kw2);
			lo_dp.AddProperty("ls_contents", typeof(string), kw3);

			using (Mny2020r_NT _Mny2020r_NT = new Mny2020r_NT())
			{
				lo_ds.Merge(_Mny2020r_NT.GetMonthlyTotalAmountByContents(lo_dp));
				li_count = DataSetUtil.GetRowCount(lo_ds, "UPDMS_MNY_REPORT");
			}

			for (int i = 0; i < li_count; i++)
			{
				int li_amt = DataSetUtil.GetFldInt(lo_ds, "UPDMS_MNY_REPORT", i, "Amount");
				int li_cnt = DataSetUtil.GetFldInt(lo_ds, "UPDMS_MNY_REPORT", i, "Cnt");

				li_total_amount += li_amt;
				li_total_count += li_cnt;

				//수동으로 데이터 바인딩
				chart_line.Data[0, i] = li_amt;
				chart_line.Data[1, i] = li_cnt;

				//X축 라벨링
				string ls_base_dt = "'" + DataSetUtil.GetFldStr(lo_ds, "UPDMS_MNY_REPORT", i, "Base_Dt").Substring(2, 5).Replace("-", ".");
				chart_line.AxisX.Labels[i] = ls_base_dt;

				//0은 안보여준다
				chart_line.Points[0, i].PointLabels.Visible = (chart_line.Data[0, i] == 0) ? false : true;
				chart_line.Points[1, i].PointLabels.Visible = (chart_line.Data[1, i] == 0) ? false : true;
			}

			GridView1.DataSource = lo_ds;
			GridView1.DataBind();
		}

		//검색
		private void SearchProcess()
		{
			gs_page_mode = "R";
			lbl_search_flag.Text = "SEARCH";
			SetSearchParameter();

			DrawMonthlyTotalAmountByContents();
		}

		#endregion

		#region Page_PreRender, ChangePageMode

		// Page_PreRender
		private void Page_PreRender(object sender, System.EventArgs e)
		{
			if (!Page.IsPostBack)
			{
				using (AccessCheck access = new AccessCheck())
				{
					//접근권한 체크
					if (!access.CheckPgmAccessGrant(PID, USER_ID, ACCESS_LOG_INFO))
					{
						Response.Redirect(URL_NO_AUTHORITY_REDIRECT_BASE);	//사용자의 권한은 요청하신 페이지를 볼 수 없습니다
					}
				}
			}
			ChangePageMode();
		}

		// ChangePageMode
		private void ChangePageMode()
		{
			switch (gs_page_mode)
			{
				case "":	// 처음 페이지로드시
					table_chart.Visible = false;
					break;
				case "R":	// 검색버튼 클릭시
					table_chart.Visible = true;
					break;
			}
			SetGridViewHeader(global.GetColumnListByPgmId(PID, LANG_SET));
		}

		#endregion

		#region GridView이벤트

		//RowDataBound
		protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
		{
			if (e.Row.RowType == DataControlRowType.DataRow)
			{
				//마우스올렸을 때
				e.Row.Attributes.Add("onMouseOver", GRID_ONMOUSEOVER_COLOR);
				e.Row.Attributes.Add("onMouseOut", GRID_ONMOUSEOUT_COLOR);

				e.Row.Visible = false;
			}

			//Footer에 합계 바인딩
			if (e.Row.RowType == DataControlRowType.Footer)
			{
				e.Row.Cells[0].Attributes.Add("class", "t_right");
				e.Row.Cells[1].Attributes.Add("class", "t_right");

				e.Row.Cells[0].Text = li_total_amount.ToString("#,###,##0");
				e.Row.Cells[1].Text = li_total_count.ToString("#,###,##0");
				
			}
		}

		#endregion

		#region 버튼이벤트

		//1개월
		protected void sbtn_1_m_Click(object sender, EventArgs e)
		{
			SetYearMonth(-1);
		}

		//3개월
		protected void sbtn_3_m_Click(object sender, EventArgs e)
		{
			SetYearMonth(-3);
		}

		//6개월
		protected void sbtn_6_m_Click(object sender, EventArgs e)
		{
			SetYearMonth(-6);
		}

		//12개월
		protected void sbtn_12_m_Click(object sender, EventArgs e)
		{
			SetYearMonth(-12);
		}

		//전체
		protected void sbtn_all_m_Click(object sender, EventArgs e)
		{
			txt_search_from_dt.Text = SEARCH_YMD_MIN;
			txt_search_to_dt.Text = DateUtil.GetNow(); //오늘
		}

		//챠트
		protected void ibtn_chart_Click(object sender, EventArgs e)
		{
			SearchProcess();
		}

		#endregion		

	}
}
